package com.example.kuaizu.controller;

import com.example.kuaizu.pojo.pram.PromotionMajorPram;
import com.example.kuaizu.pojo.pram.PromotionSchoolPram;
import kuaizu.common.exception.MassageException;
import com.example.kuaizu.pojo.*;
import com.example.kuaizu.pojo.dto.UserEmailDao;
import com.example.kuaizu.pojo.ov.HouProjectOv;
import com.example.kuaizu.pojo.ov.PromotionArrayObjectOv;
import com.example.kuaizu.service.InEmailPromotionService;
import com.example.kuaizu.service.InInterestedService;
import com.example.kuaizu.utils.AjaxResponse;
import kuaizu.common.param.PromotionInfoParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.util.Set;

import static kuaizu.common.constp.KuaiZuConst.ERROR;

@Log4j2
@RestController
@RequestMapping("/email")
public class EmailPromotionController {
    @Autowired
    InEmailPromotionService emailPromotionService;
    @Autowired
    InInterestedService interestedService;

    //邮件推广
    @PostMapping("/promotion")
    public AjaxResponse emailPromotion(@RequestBody @Validated PromotionArrayObjectOv promotionArrayObjectsOv) throws MessagingException {
        //如果没有项目id,则抛出异常
        if (promotionArrayObjectsOv.getProjectId() == null) {
            throw new MassageException(ERROR, "项目id不能为空");
        }
        //从ThreadLocal中获取用户的openid信息
        //Map<String, Object> map = ThreadLocalUtil.get();
        //String openid = (String) map.get("openid");
        //promotionArrayObjectsOv.setOpenid(openid);
        emailPromotionService.sendEmail(promotionArrayObjectsOv);
        return AjaxResponse.success("邮件推广中");
    }

    //根据学校进行邮件推广
    @PostMapping("/promotionSchool")
    public AjaxResponse promotionSchool(@RequestBody @Validated PromotionSchoolPram pram){
        emailPromotionService.sendEmailBySchoolList(pram);
        return AjaxResponse.success("邮件推广中");
    }

    //根据学校进行邮件推广
    @PostMapping("/promotionMajor")
    public AjaxResponse promotionMajor(@RequestBody @Validated PromotionMajorPram pram){
        emailPromotionService.sendEmailByMajorList(pram);
        return AjaxResponse.success("邮件推广中");
    }
    //查看对应的邮件推广详情
    @PostMapping("/promotionProject")
    public AjaxResponse promotionInfo(@RequestBody @Validated PromotionInfoParam req) {
        PageBean<HouProjectOv> promotionInfo = emailPromotionService.getPromotionInfo(req);
        return AjaxResponse.success(promotionInfo);
    }
    //根据项目id集合查询项目详情
    @PostMapping("/promotionProjectByIds")
    public AjaxResponse promotionInfoByIds(@RequestBody @Validated PromotionInfoIdsParam req) {
        PageBean<HouProjectOv> promotionInfo = emailPromotionService.getPromotionInfoByIds(req);
        return AjaxResponse.success(promotionInfo);
    }


    //查看可能对这个项目感兴趣的人
    @GetMapping("/interestedpeople/{id}")
    public AjaxResponse interested(@PathVariable Integer id) {
        Set<UserEmailDao> user = interestedService.getInterestedUser(id);
        if (user.isEmpty()) {
            return AjaxResponse.success("你说这么好的项目怎么没有人感兴趣呢???");
        }
        return AjaxResponse.success(user);
    }

    //TODO根据我自己的专业查看可能感兴趣的项目
    @GetMapping("/majorbyproject/{id}")
    public AjaxResponse promotion(@PathVariable Integer id) {
        Set<UserEmailDao> user = emailPromotionService.majorbyproject(id);
        return AjaxResponse.success(null);
    }

}
